package cn.devhost;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author shenguangyang
 */
@Slf4j
@Component
public class Runner implements ApplicationRunner {
    @Resource
    private NetworkService networkService;
    @Resource
    private ServerProperties properties;

    @Override
    @SuppressWarnings("all")
    public void run(ApplicationArguments args) throws Exception {
        new Thread(() -> {
            while (true) {
                try {
                    List<NetworkInfo> networkInfoList = NetworkUtils.getNetWorkInfo(properties.getNetworkInterfaces());
                    networkService.sendEmail(networkInfoList);
                } catch (Exception e) {
                    log.error("error: ", e);
                } finally {
                    try {
                        TimeUnit.SECONDS.sleep(20);
                    } catch (Exception e) {
                       log.error("error: ", e);
                    }
                }
            }
        }).start();
    }
}
